package com.campus.controller;

import com.campus.common.Result;
import com.campus.entity.EmailVO;
import com.campus.entity.PasswordVO;
import com.campus.entity.User;
import com.campus.entity.Users;
import com.campus.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

// 用户控制器
// TODO: 实现用户相关接口
@RestController
@CrossOrigin("http://localhost:3000/")
@RequestMapping("/api")
public class UserController {
    @Autowired
    private UserService userService;
    @PutMapping("/user/update")
    public Result updateUser(@RequestBody Users user){
    return userService.updateUser(user);
    }
    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/user/register")
    public Result register(@RequestBody User user) {

        return userService.register(user);
    }
    /**
     * 用户登录
     */
    @PostMapping("/user/login")
    public Result login(@RequestBody User user, HttpServletResponse response, HttpServletRequest request, HttpSession session){
        return userService.login(user,response,request,session);
    }
    /**
     * 用户登出
     */
    @PostMapping("/user/logout")
    public Result logout(HttpServletRequest request){
        //退出登录
        Result result = userService.logout(request);
        return result;
    }
    /**
     *
     * 用户忘记密码功能--给邮箱发送验证码
     */
    @PostMapping("/user/forgot-password/send-code")
    public Result sendCode(@RequestBody EmailVO email){
        return userService.sendCode(email);
    }
    /**
     * 校验验证码
     */
    @PostMapping("/user/forgot-password/verify-code")
    public Result verifyCode(@RequestBody EmailVO emailVO) {
        return userService.verifyCode(emailVO);
    }
    /**
     * 重置密码
     */
    @PostMapping("/user/forgot-password/reset")
    public Result resetPassword(@RequestBody EmailVO emailVO) {
        return userService.resetPassword(emailVO);
    }

    /**
     * 用户登录后，通过旧密码进行密码的修改
     */
    @PutMapping("/user/password")
    public Result updatePassword(@RequestBody PasswordVO passwordVO,HttpServletRequest request) {
        return userService.updatePwdByOldPwd(passwordVO,request);
    }

    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/user/info")
    public Result getUserInfo(HttpServletRequest request) {
        return userService.getUserInfo(request);
    }

    /**
     * 获取验证码以及请求验证码
     */
    @GetMapping("/user/captcha")
    public Result getCaptcha() {
        return userService.getCaptcha();
    }
    /**
     * 处理预检请求
     */
    @RequestMapping(value = "/**", method = RequestMethod.OPTIONS)
    public void handleOptions(HttpServletResponse response) {
        response.setStatus(HttpServletResponse.SC_OK);
    }
    /**
     * 注册发送验证码
     */
    @PostMapping("/user/register/send-code")
    public Result getRegisterCode(@RequestBody EmailVO emailVO){
        Result result = userService.getRegisterCode(emailVO);
        return result;
    }
}

